﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using WPFDiagramming.Interfaces;
using WPFDiagramming.Adorners;

namespace WPFDiagramming.Controls
{
    public class DiagramPanel : Canvas
    {

        public event RoutedEventHandler Selected
        {
            add { AddHandler(ControlBase.SelectedEvent, value); }
            remove { RemoveHandler(ControlBase.SelectedEvent, value); }
        }


        private UIElement _selectedElement = null;

        private bool hasSelectedElement = false;

        private List<UIElement> _selectedElements = null;
        public List<UIElement> SelectedElements
        {
            get
            {
                return this._selectedElements;
            }
        }

        public DiagramPanel()
        {
            this.Background = Brushes.White;
            _selectedElements = new List<UIElement>();

            this.Focusable = true;

            this.Selected += OnChildSelected;
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (e.Key == Key.Delete)
            {
                foreach (var item in this.SelectedElements)
                {
                    var source = (FrameworkElement)item;
                    var parent = (DiagramPanel)source.Parent;

                    if (parent != null)
                    {
                        parent.Children.Remove(source);
                    }
                }
                
                this.CleanSelectedItems();
            }
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            Keyboard.Focus(this);

            if (e.OriginalSource == this)
                this.RemoveAnySelection();
        }

        private void OnChildSelected(object sender, RoutedEventArgs e)
        {
            KeyStates key = Keyboard.GetKeyStates(Key.LeftShift);
            if (key != KeyStates.Down && key != (KeyStates.Down | KeyStates.Toggled))
                this.RemoveAnySelection();

            if (e.Source is ControlBase)
            {
                this._selectedElement = (UIElement)e.Source;
                this._selectedElements.Add(this._selectedElement);

                this.hasSelectedElement = true;
            }
        }

        public void RemoveAnySelection()
        {
            if (hasSelectedElement)
            {
                foreach (var selectedElement in this.SelectedElements)
                {
                    if (selectedElement is ControlBase)
                    {
                        ((ControlBase)selectedElement).RemoveAdorners();
                    }
                }
                this.CleanSelectedItems();
            }
        }

        private void CleanSelectedItems()
        {

            _selectedElement = null;
            _selectedElements.Clear();
            hasSelectedElement = false;
        }
    }
}
